package com.ptswitch.material.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.hxzon.util.EMailUtil.EMail;
import org.hxzon.util.ExcelUtil;

import com.ptswitch.material.model.base.Upload;
import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractInvoice;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.model.contract.ContractPayment;
import com.ptswitch.material.model.material.Bom;
import com.ptswitch.material.model.material.Material;

public class MaterialUtil {
    private static Properties properties;

    public static void init(String contextRealPath) {
        properties = new Properties();
        try {
            properties.load(FileUtils.openInputStream(new File(contextRealPath + "/WEB-INF/app.properties")));
        } catch (IOException e) {
        }
    }

    public static EMail newEMail() {
        EMail email = new EMail();
        email.setHost(properties.getProperty("mstar.email.host"));
        email.setFrom(properties.getProperty("mstar.email.from"));
        email.setUsername(properties.getProperty("mstar.email.username"));
        email.setPassword(properties.getProperty("mstar.email.password"));
        email.setEncoding("UTF-8");
        //		email.addCharset("charset=utf8");
        //		email.addCharset("charset=gbk");
        return email;
    }

    public static Material toMaterial(String code, String name, String spec, String desc, String pcb, String state, String foot, String brand, String supplier, String remark) {
        Material m = new Material();
        m.setCode(code == null ? "" : code);
        m.setName(name == null ? "" : name);
        m.setSpecification(spec == null ? "" : spec);
        m.setDescription(desc == null ? "" : desc);
        m.setPcbDecal(pcb == null ? "" : pcb);
        m.setState(state == null ? "" : state);
        m.setFootprint(foot == null ? "" : foot);
        m.setBrand(brand == null ? "" : brand);
        m.setSupplier(supplier == null ? "" : supplier);
        m.setRemark(remark == null ? "" : remark);
        return m;
    }

    public static Object[] toUrl(Material m, long size, long first) {
        if (m == null) {
            return null;
        }
        return new Object[] { m.getCode(), m.getName(), m.getSpecification(), m.getDescription(), m.getPcbDecal(), m.getState(), m.getFootprint(), m.getBrand(), m.getSupplier(), m.getRemark(), size,
                first };
    }

    public static List<ContractItem> toContractItems(List<Material> materials) {
        List<ContractItem> contractItems = new ArrayList<ContractItem>();
        ContractItem item;
        for (Material material : materials) {
            item = toContractItem(material);
            contractItems.add(item);
        }
        return contractItems;
    }

    public static ContractItem toContractItem(Material material) {
        ContractItem contractItem = new ContractItem();
        contractItem.setCode(material.getCode());
        contractItem.setBrand(material.getBrand());
        contractItem.setFootprint(material.getFootprint());
        contractItem.setName(material.getName());
        contractItem.setPcbDecal(material.getPcbDecal());
        contractItem.setSpecification(material.getSpecification());
        return contractItem;
    }

    public static String toCheckboxLabel(Material material) {
        return material.toString();
    }

    public static String toCheckboxLabel(ContractItem contractItem) {
        return contractItem.toString();
    }

    //html
    public static String linkForViewMaterial(String webUrl, Material material) {
        return "<a href='" + webUrl + "/material/view/" + material.getId() + "' >物料：" + material.getCode() + "</a>";
    }

    public static String linkForViewContract(String webUrl, Contract contract) {
        return "<a href='" + webUrl + "/contract/view/" + contract.getId() + "' >合同：" + contract.getCode() + "</a>";
    }

    public static String linkForDeleteContract(String webUrl, Contract contract) {
        return "<a href='" + webUrl + "/contract/delete/" + contract.getId() + "' >删除合同：" + contract.getCode() + "</a>";
    }

    public static String linkForResetContract(String webUrl, Contract contract) {
        return "<a href='" + webUrl + "/contract/reset/" + contract.getId() + "' >激活合同：" + contract.getCode() + "</a>";
    }

    public static String linkForReplyPayment(String webUrl, ContractPayment payment) {
        return "<a href='" + webUrl + "/contract/replyPayment/" + payment.getId() + "'>回复付款申请</a>";
    }

    public static String linkForReplyRefund(String webUrl, ContractPayment payment) {
        return "<a href='" + webUrl + "/contract/replyRefund/" + payment.getId() + "'>回复退款申请</a>";
    }

    public static String linkForReplyInvoice(String webUrl, ContractInvoice invoice) {
        return "<a href='" + webUrl + "/contract/replyInvoice/" + invoice.getId() + "'>回复开具发票</a>";
    }

    //export info
    public static StringBuilder buildMessage(Material material) {
//		overview+=material.getCode();
        StringBuilder message = new StringBuilder();
        message.append("物料编码:").append(material.getCode()).append(";\n");
        message.append("物料名称:").append(material.getName()).append(";\n");
        message.append("规格型号:").append(material.getSpecification()).append(";\n");
        message.append("器件封装:").append(material.getFootprint()).append(";\n");
        message.append("品牌:").append(material.getBrand()).append(";\n");
        message.append("器件描述:").append(material.getDescription()).append(";\n");
        message.append("供货商:").append(material.getSupplier()).append(";\n");
        message.append("pcb封装:").append(material.getPcbDecal()).append(";\n");
        message.append("备注:").append(material.getRemark()).append(";\n");
        message.append("采购状态:").append(material.getState()).append(";\n");
        return message;
    }

    public static StringBuilder buildMessage(Bom bom) {
//		overview+=bom.getCode();
        StringBuilder message = new StringBuilder();
        message.append("单板名:").append(bom.getCode()).append(";\n");
        message.append("版本:").append(bom.getVersion()).append(";\n");
        message.append("文件名:").append(bom.getFileName()).append(";\n");
        return message;
    }

    public static StringBuilder buildMessage(Uzer userInfo) {
//		overview+=userInfo.getUsername();
        StringBuilder message = new StringBuilder();
        message.append("用户名:").append(userInfo.getUsername()).append(";\n");
        return message;
    }

    public static StringBuilder buildMessage(Upload upload) {
        StringBuilder message = new StringBuilder();
        message.append("文件名：").append(upload.getFileName()).append(";\n");
        message.append("文件路径：").append(upload.getFilePath()).append(";\n");
        return message;
    }

    public static StringBuilder buildMessage(Contract contract) {
        StringBuilder message = new StringBuilder();
        message.append("合同编码：").append(contract.getCode()).append(";\n");
        message.append("合同供应商：").append(contract.getSupplier()).append(";\n");
        message.append("合同状态：").append(contract.getStatus()).append(";\n");
        return message;
    }

    public static StringBuilder buildMessageForExamineContractItem(List<ContractItem> items) {
        StringBuilder message = new StringBuilder();
        message.append("本次验货详情：");
        message.append("<table border='1' cellpadding='1' cellspacing='1'>");
        for (ContractItem item : items) {
            message.append("<tr>");
            message.append("<td>");
            message.append(item.getCode());
            message.append("</td>");
            message.append("<td>");
            message.append("通过：" + item.getNumCurrentPass());
            message.append("</td>");
            message.append("<td>");
            message.append("未通过：" + item.getNumCurrentUnPass());
            message.append("</td>");
            message.append("<td>");
            message.append("未验：" + item.getNumUnExamine());
            message.append("</td>");
            message.append("</tr>");
        }
        message.append("</table>");
        return message;
    }

    public static StringBuilder buildMessageForReceivingContractItem(String webUrl, Contract contract, List<ContractItem> items) {
        StringBuilder message = new StringBuilder();
        message.append("本次收货详情：");
        message.append("<table border='1' cellpadding='1' cellspacing='1'>");
        for (ContractItem item : items) {
            message.append("<tr>");
            message.append("<td>");
            message.append(item.getCode());
            message.append("</td>");
            message.append("<td>");
            message.append("本次收货：" + item.getNumCurrentReceive());
            message.append("</td>");
            message.append("<td>");
            message.append("未验：" + item.getNumUnExamine());
            message.append("</td>");
            message.append("</tr>");
        }
        message.append("</table>");
        message.append("<a href='" + webUrl + "/contract/examineitems/" + contract.getId() + "'>验货</a>");
        return message;
    }

    public static ColumnMap buildColumnMap(Sheet sheet) {
        ColumnMap columnMap = new ColumnMap();
        Row row = sheet.getRow(0);
        int colNum = row.getLastCellNum();
        String value = null;
        for (int i = 0; i < colNum; i++) {
            value = ExcelUtil.getCellString(sheet, 0, i);
            if (!value.isEmpty()) {
                columnMap.put(value, i);
                // logger.debug(value + ":" + i);
            }
        }
        return columnMap;
    }

    public static String getCellString(Row row, String columnName, ColumnMap columnMap) {
        return ExcelUtil.getCellString(row, columnMap.get(columnName));
    }
}
